<template>
  <div class="corper">
    <div class="corper-main">
      <div class="main">
        <mpvue-cropper
          ref="cropper"
          :option="cropperOpt"
          @ready="cropperReady"
          @beforeDraw="cropperBeforeDraw"
          @beforeImageLoad="cropperBeforeImageLoad"
          @beforeLoad="cropperLoad"
        ></mpvue-cropper>
      </div>
      <div class="options">
        <div class="btn cancel" @tap="cancelCropper">取消裁剪</div>
        <div class="btn ok" @tap="getCropperImage">开始制作</div>
      </div>

    </div>

  </div>

</template>

<script>
  import MpvueCropper from 'mpvue-cropper'
  let wecropper;
  const device = wx.getSystemInfoSync();
  const width = device.windowWidth;
  const height = device.windowHeight - 75;
  export default {
    name: "Croper",
    props:{
      imgUrl:{
        type:String,
        value:""
      }
    },
    data () {
      return {
        cropperOpt: {
          id: 'cropper',
          targetId: 'targetCropper',
          pixelRatio: device.pixelRatio,
          width,
          height,
          scale: 2.5,
          zoom: 8,
          cut: {
            x: (width - 300) / 2,
            y: (height - 300) / 2,
            width: 300,
            height: 300
          }
        }
      }
    },
    components: {MpvueCropper},
    methods:{
      cropperReady (...args) {
        console.log('cropper ready!')
      },
      cropperBeforeImageLoad (...args) {
        console.log('before image load')
      },
      cropperLoad (...args) {
        console.log('image loaded')
      },
      cropperBeforeDraw (...args) {
        // Todo: 绘制水印等等
      },
      cancelCropper () {
        wecropper = null;
        this.$emit("cancelCropper",{});
      },
      getCropperImage () {
        let _this = this;
        wecropper.getCropperImage({ original: true })
          .then((src) => {
            console.log(src);
            mpvue.getFileSystemManager().readFile({
              filePath: src, //选择图片返回的相对路径
              encoding: 'base64', //编码格式
              success: res => { //成功的回调
                console.log(res.data);
                _this.$emit('comicsFusion',{'local':src,'base64':res.data});
                //that.base64 = res.data;
              }
            })
            // wx.previewImage({
            //   current: '', // 当前显示图片的http链接
            //   urls: [src] // 需要预览的图片http链接列表
            // })
          })
          .catch(e => {
            console.error('获取图片失败')
          })
      }
    },
    mounted () {
      wecropper = this.$refs.cropper;
      console.log(this.imgUrl);
      wecropper.pushOrigin(this.imgUrl);
    }
  }
</script>

<style scoped>
  .corper{
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.8);
    position: absolute;
    bottom: 0;
    left:0;
    z-index: 100;
  }
  .corper .corper-main{
    width: 100%;
    height: 100%;
    position: absolute;
    bottom: 0;
    left:0;
    z-index: 100;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;

  }
  .corper .corper-main .main{
    width: 100%;
    margin: 0 auto;
    height: 100%;
    position: relative;
    background-color: rgba(0,0,0,.8);
  }
  .corper .corper-main .options{
    position: fixed;
    bottom: 25px;
    left: 0;
    right: 0;
    height: 50px;
    padding: 0 10px;
    box-sizing: border-box;
    line-height: 50px;
    width: 90%;
    margin: 0 auto;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    align-content: stretch;
    z-index:99999;
    background-color: rgba(0,0,0,.8);
  }
  .corper .corper-main .options .btn{
    height: 30px;
    line-height: 30px;
    padding: 0 24px;
    border-radius: 2px;
    color: #ffffff;
  }
  .corper .corper-main .options .btn.cancel{
    background-image: linear-gradient(to right top, #ec75b1, #f083ba, #f591c3, #f99fcb, #fdacd4);
  }
  .corper .corper-main .options .btn.ok{
    background-image: linear-gradient(to right top, #4a94e1, #569ce5, #62a4ea, #6dacee, #78b4f2);
  }


</style>
